---
title: "Dates, Times, and Groups"
output:
  ioslides_presentation: 
    smaller: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Dates and Times

- Dates and times are deceptively tricky to work with
- Formats - Is 02/05/2017 February 5 or May 2?
- Time Zones
- POSIXct and POSIXlt format in R is difficult to work with

##Lubridate Package

  - available from CRAN (`install.packages("lubridate")`)
  - Written by Garett Grolemund and Hadley Wickham
  - associated paper
    - *JSS: Dates and Times made easy with lubridate*
    - http://www.jstatsoft.org/v40/i03/paper


## Instants of Time Examples

```{r,message = F}
library(lubridate)
```

One moment in time, usually named, e.g.
```{r}
now() # Date with time

as.Date(now()) # Just the date
```

##

Libridate turns strings into instants with functions that have y, m, and d in their names

```{r}
ymd("2013-05-14")

mdy("05/14/2013")

dmy("14052013")

ymd_hms("2013:05:14 14:50:30")
```

Order matters!

## Working with Instants

Standard arithmetic operations now work on dates:
```{r}
ymd("2017-07-23") > ymd("1970-01-01")
myd("07-2017-23") - ymd("1970-01-01")
```


## Functions for extracting pieces of dates:
```{r}
month(now())

wday(now())

wday(now(), label=TRUE)

```



## Your Turn

1. The last time the Cleveland Indians won the World Series of Baseball was October 11th, 1948. How long has it been in days?
2. Wayne Gretzky's first NHL game was 1980-04-08 (ymd) and his last was 1999-04-18 (ymd). How many days was he an NHL player?

## Answers

### 1.
```{r}
as.Date(now()) - mdy("10-11-1948")
```

### 2.
```{r}
ymd("1999-04-18") - ymd("1980-04-08")
```

## Accessor Functions

Component        Function      
---------------- --------
Year             `year()`
Month            `month()`
Day of the year  `yday()`
Day of the month `mday()`
Day of the week  `wday()`
Hour             `hour()`
Minute           `minute()`
Second           `second()`
Time zone        `tz()`



## Example
What day of the week did the Boston Celtics play their first game in the 2008 NBA Playoffs?
```{r}
nba.playoffs <- read.csv("NBA Playoffs.csv")
head(nba.playoffs, n = 2)
```

##

```{r}
#wday() will tell us which day of the week this date is
wday("2008-04-20",label = TRUE)
```


## Your Turn

1. What day is most common for NBA playoff games? (Hint: Make a bar plot and use the `as.Date(mdy())` command)

## Answers

### 1. 
Sunday
```{r,fig.height=2, fig.width=7}
library(ggplot2)
nba.playoffs <- read.csv("NBA Playoffs.csv")

playoffs.dates <- nba.playoffs$Date #Creates vector of dates
playoffs.dates <- as.Date(mdy(playoffs.dates)) # Changes structure
playoffs.dates <- wday(playoffs.dates, label = T) # Computes day of week

qplot(playoffs.dates, geom = "bar",
      main = "Barplot of Days for NBA Playoff Games")

```


## What is a Time Series?

-  series of data points sequenced with a time ordering 

## Example

Time series of points scored by Boston Celtics in 2008 NBA Playoffs using `ggplot()`

```{r}
boston <- read.csv("Boston Celtics.csv")
#Convert date to a date structure type
boston$Date <- as.Date(boston$Date,"%m/%d/%Y")
```

##

```{r,fig.height=3, fig.width=7}
ggplot(data = boston,aes(x = Date,y = Points,group = 1)) + 
  geom_point() + 
  geom_line() + 
  #For date_labels we use the strptime notation
  scale_x_date(name = 'Date', # Creates x-axis scale
               date_breaks = '4 days',
                date_labels = '%a') +
  ggtitle("Time Series for Points Scored by Boston Celtics in 2008 Playoffs")
```

From this time series plot, we can see that the Celtics scored less points in the middle stages of the playoffs. This could perhaps be due to some fatigue factor or strength of opponent.


## Your Turn

1. Make time series plot for the 2008 Boston Celtics Playoff run for their team assists (Team.AST).

2. What can you interpret from this?

## Answers
### 1.
```{r,fig.height=2, fig.width=7}
library(ggplot2)
boston <- read.csv("Boston Celtics.csv")
#Convert date to a date structure type
boston$Date <- as.Date(boston$Date,"%m/%d/%Y")

ggplot(data = boston,aes(x = Date,y = Team.AST,group = 1)) + 
  geom_point() + 
  geom_line() + 
  #For date_labels we use the strptime notation
  scale_x_date(name = 'Date', date_breaks = '4 days',
        date_labels = '%a') +
  ggtitle("Time Series for Team Assists by Boston Celtics in 2008 Playoffs")
```

##

### 2.
` As with the time series of points scored, we see there are less total assists in the middle stages of the playoffs. This is most likely a result of the strength of the opponent of some other explainable factors.`
